<?php
namespace app\massage\controller;
use app\AdminRest;
use app\massage\model\CarAtvList;
use app\massage\model\CarDriver;
use app\massage\model\CarTrophy;
use app\massage\model\CarType;
use app\massage\model\CarUserTrophy;
use app\massage\model\Coach;
use app\massage\model\CoachLevel;

use app\massage\model\Goods;
use app\massage\model\Order;
use app\massage\model\Police;
use app\massage\model\User;
use app\member\model\Level;
use app\restaurant\model\GoodsCate;
use app\restaurant\model\GoodsSpe;
use app\shop\model\Wallet;
use longbingcore\wxcore\PospalApi;
use longbingcore\wxcore\WxPay;
use think\App;
use app\shop\model\Order as Model;
use think\facade\Db;


class AdminDriver extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $trophy_model;

    protected $car_type_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new CarDriver();

        $this->car_type_model = new CarType();
//
        $this->trophy_model = new CarTrophy();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:43
     * @功能说明:列表
     */
    public function driverList(){


        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }else{

            $dis[] = ['a.status','>',-1];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];

        }

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['user_name','like','%'.$input['name'].'%'];

            $where[] = ['mobile','like','%'.$input['name'].'%'];
        }

        $data = $this->model->dataList($dis,$input['limit'],$where);

        $list = [

           '0'=>'all',

            1=>'ing',

            2=>'pass',

            4=>'nopass'
        ];

        foreach ($list as $k=> $value){

            $dis_s = [];

            $dis_s[] =['uniacid','=',$this->_uniacid];

            if(!empty($k)){

                $dis_s[] = ['status','=',$k];

            }else{

                $dis_s[] = ['status','>',-1];

            }

            $data[$value] = $this->model->where($dis_s)->count();

        }

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:58
     * @功能说明:车手详情
     */
    public function driverInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $user_model = new User();

        $data['nickName'] = $user_model->where(['id'=>$data['user_id']])->value('nickName');


        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-07-03 00:15
     * @功能说明:审核(2通过,3取消,4拒绝)
     */
    public function driverUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        if(!empty($input['status'])&&in_array($input['status'],[2,4])){

            $input['sh_time'] = time();
        }

        $data = $this->model->dataUpdate($dis,$input);

        return $this->success($data);

    }





    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:53
     * @功能说明:奖杯列表
     */
    public function trophyList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $data = $this->trophy_model->dataList($dis,$input['limit']);

        return $this->success($data);


    }

    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:53
     * @功能说明:奖杯列表
     */
    public function trophySelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        if(!empty($input['user_id'])){

            $user_t_model = new CarUserTrophy();

            $id = $user_t_model->where(['user_id'=>$input['user_id']])->column('trophy_id');

            $dis[] = ['id','in',$id];
        }

        $data = $this->trophy_model->where($dis)->select()->toArray();

        return $this->success($data);


    }

    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:56
     * @功能说明:添加奖杯
     */
    public function trophyAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->trophy_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:57
     * @功能说明:编辑奖杯
     */
    public function trophyUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->trophy_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:59
     * @功能说明:奖杯详情
     */
    public function trophyInfo(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->trophy_model->dataInfo($dis);

        return $this->success($res);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:53
     * @功能说明:车型列表
     */
    public function cartypeSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $data = $this->car_type_model->where($dis)->select()->toArray();

        return $this->success($data);


    }

    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:53
     * @功能说明:车型列表
     */
    public function cartypeList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->car_type_model->dataList($dis,$input['limit']);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:56
     * @功能说明:添加车型
     */
    public function cartypeAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->car_type_model->dataAdd($input);

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:57
     * @功能说明:编辑车型
     */
    public function cartypeUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];
        //删除
        if(isset($input['status'])&&in_array($input['status'],[-1,0])){

            $goods_model = new Goods();

            $goods = $goods_model->carTypeHave($input['id']);

            if(!empty($goods)){

                $this->errorMsg('产品：'.$goods['title'].' 正在使用该车型，无法删除或下架');
            }

            $atv_model = new CarAtvList();

            $atv = $atv_model->where(['car_type_id'=>$input['id']])->where('status','>',-1)->find();

            if(!empty($atv)){

                $atv = $atv->toArray();

                $this->errorMsg('活动：'.$atv['title'].' 正在使用该车型，无法删除或下架');
            }

        }

        $res = $this->car_type_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:59
     * @功能说明:车型详情
     */
    public function cartypeInfo(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->car_type_model->dataInfo($dis);

        return $this->success($res);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-07-07 18:46
     * @功能说明:提现列表
     */
    public function walletList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.type','=',$input['type']];

        if(!empty($input['code'])){

            $dis[] = ['a.code','like','%'.$input['code'].'%'];
        }

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];
        }

        $data = $this->wallet_model->adminList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-07 18:57
     * @功能说明:提现详情
     */
    public function walletInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->wallet_model->dataInfo($dis);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-07 18:58
     * @功能说明:通过提现申请
     */
    public function walletPass(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->wallet_model->dataInfo($dis);

        if($data['status']!=1){

            $this->errorMsg('申请已审核');
        }

        $update = [

            'sh_time'   => time(),

            //'text'      => !empty($input['text'])?$input['text']:'',

            'status'    => 2,

            'online'    => $input['online'],

            'true_price'=> $data['apply_price']
        ];

        Db::startTrans();

        $res = $this->wallet_model->dataUpdate(['id'=>$input['id'],'status'=>1],$update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('打款失败');

        }
        //线上转账
        if($input['online']==1){

            $user_model = new \app\massage\model\User();

            $openid = $user_model->where(['id'=>$data['user_id']])->value('openid');

            if(empty($openid)){

                return $this->error('用户信息错误，未获取到openid');
            }
            //微信相关模型
            $wx_pay = new WxPay($this->_uniacid);
            //微信提现
            $res    = $wx_pay->crteateMchPay($this->payConfig(),$openid,$update['true_price']);

            if($res['result_code']=='SUCCESS'&&$res['return_code']=='SUCCESS'){

                if(!empty($res['payment_no'])){

                    $this->wallet_model->dataUpdate(['id'=>$input['id']],['payment_no'=>$res['payment_no']]);
                }

            }else{

                Db::rollback();

                return $this->error(!empty($res['err_code_des'])?$res['err_code_des']:'你还未该权限');

            }

        }

        Db::commit();

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-26 15:03
     * @功能说明:决绝提现
     */
    public function walletNoPass(){

        $input = $this->_input;

        $info = $this->wallet_model->dataInfo(['id'=>$input['id']]);

        if($info['status']==2){

            $this->errorMsg('已同意打款');
        }

        if($info['status']==3){

            $this->errorMsg('已拒绝打款');
        }

        Db::startTrans();


        $update = [

            'sh_time' => time(),

            //'text'      => !empty($input['text'])?$input['text']:'',

            'status'    => 3,

        ];

        $res = $this->wallet_model->dataUpdate(['id'=>$input['id'],'status'=>1],$update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('打款失败');

        }

        $cap_info = $this->model->dataInfo(['id'=>$info['coach_id']]);

        if($info['type']==1){

            $res = $this->model->dataUpdate(['id'=>$info['coach_id']],['service_price'=>$cap_info['service_price']+$info['total_price']]);
        }else{

            $res = $this->model->dataUpdate(['id'=>$info['coach_id']],['car_price'=>$cap_info['car_price']+$info['total_price']]);

        }

        if($res!=1){

            Db::rollback();

            $this->errorMsg('审核失败');

        }

        Db::commit();


        return $this->success($res);

    }











}
